package com.shangcheng.server.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shangcheng.dubbo.api.SmsCouponApi;
import com.shangcheng.dubbo.api.SmsCouponProductCategoryRelationApi;
import com.shangcheng.dubbo.api.SmsCouponProductRelationApi;
import com.shangcheng.model.domain.SmsCoupon;
import com.shangcheng.model.domain.SmsCouponProductCategoryRelation;
import com.shangcheng.model.domain.SmsCouponProductRelation;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.result.SmsCouponVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsCouponService {
    @DubboReference
    private SmsCouponApi couponApi;

    @DubboReference
    private SmsCouponProductRelationApi couponProductRelationApi;

    @DubboReference
    private SmsCouponProductCategoryRelationApi couponProductCategoryRelationApi;

    public PageResult list(String name, Integer type, Integer pageNum, Integer pageSize) {
        IPage<SmsCoupon> pg = couponApi.list(name, type, pageNum, pageSize);
        return new PageResult(Convert.toInt(pg.getTotal()), pageSize, Convert.toInt(pg.getPages()), pageNum, pg.getRecords());
    }

    public void create(SmsCouponVo couponVo) {
        //保存优惠券
        SmsCoupon smsCoupon = new SmsCoupon();
        BeanUtil.copyProperties(couponVo, smsCoupon);
        SmsCoupon coupon = couponApi.create(smsCoupon);

        //保存优惠券与商品关系
        List<SmsCouponProductRelation> productRelationList = couponVo.getProductRelationList();
        if (ObjectUtil.isNotEmpty(productRelationList)) {
            couponProductRelationApi.create(coupon.getId(), productRelationList);
        }

        //保存优惠券与商品分类关系
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = couponVo.getProductCategoryRelationList();
        if (ObjectUtil.isNotEmpty(productCategoryRelationList)) {
            couponProductCategoryRelationApi.create(coupon.getId(), productCategoryRelationList);
        }
    }

    public SmsCouponVo findById(Integer id) {
        SmsCouponVo smsCouponVo = new SmsCouponVo();
        //根据id查看优惠券详情
        SmsCoupon coupon = couponApi.findById(id);
        //根据couponId查看优惠券与商品关系
        List<SmsCouponProductRelation> productRelationList = couponProductRelationApi.findById(id);
        //根据couponId查看优惠券与商品分类关系
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = couponProductCategoryRelationApi.findById(id);

        BeanUtil.copyProperties(coupon, smsCouponVo);
        smsCouponVo.setProductRelationList(productRelationList);
        smsCouponVo.setProductCategoryRelationList(productCategoryRelationList);
        return smsCouponVo;
    }

    public void update(Integer id, SmsCouponVo smsCouponVo) {
        SmsCoupon coupon = new SmsCoupon();
        BeanUtil.copyProperties(smsCouponVo, coupon);
        couponApi.updateByCouponId(coupon);

        //更新优惠券与商品关系
        List<SmsCouponProductRelation> productRelationList = smsCouponVo.getProductRelationList();
        couponProductRelationApi.updateByCouponId(id, productRelationList);

        //更新优惠券与商品分类关系
        List<SmsCouponProductCategoryRelation> productCategoryRelationList = smsCouponVo.getProductCategoryRelationList();
        couponProductCategoryRelationApi.updateByCouponId(id, productCategoryRelationList);
    }

    public void delete(Integer id) {
        couponApi.deleteByCouponId(id);
        //删除优惠券与商品关系
        couponProductRelationApi.deleteByCouponId(id);
        //删除优惠券与商品关系
        couponProductCategoryRelationApi.deleteByCouponId(id);
    }
}
